<template>
  <div class="table-wrapper">
    <div class="search-container">
      <div class="search-wrapper">
        <span class="label">校区：</span>
        <el-select size="mini" v-model="params.is_xq" placeholder="请选择校区" @change="getBuilde" clearable>
          <el-option v-for="item in xqList" :key="item.text" :label="item.text" :value="item.value"></el-option>
        </el-select>
      </div>
      <div class="search-wrapper">
        <span class="label">楼宇：</span>
        <el-select
          v-model="params.builde_id"
          multiple
          size="mini"
          class="large-select"
          collapse-tags
          clearable
          :disabled="!params.is_xq"
          placeholder="请选择楼宇">
          <el-option
            v-for="item in buildingList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            <template slot="default">
              <label class="el-checkbox is-checked">
                <span class="el-checkbox__input " :class="{'is-checked' : params.builde_id.indexOf(item.value) > -1}">
                  <span class="el-checkbox__inner"></span>
                </span>
                <span class="el-checkbox__label">{{item.label}}</span>
              </label>
            </template>
          </el-option>
        </el-select>
      </div>
      <div class="search-wrapper">
        <span class="label">开始时间：</span>
        <el-date-picker
          size="mini"
          v-model="params.start_time"
          :type="'year'"
          :value-format="'yyyy'"
          placeholder="选择开始时间"
        >
        </el-date-picker>
      </div>
      <div class="search-wrapper">
        <span class="label">结束时间：</span>
        <el-date-picker
          size="mini"
          v-model="params.end_time"
          :type="'year'"
          :value-format="'yyyy'"
          placeholder="选择结束时间"
        >
        </el-date-picker>
      </div>
    </div>
    <div class="search-container">
      <div class="search-wrapper">
        <span class="label">图形类型：</span>
        <el-select size="mini" v-model="graphicsType" placeholder="请选择图形类型">
          <el-option v-for="item in graphicsTypes" :key="item.text" :label="item.text" :value="item.value"></el-option>
        </el-select>
      </div>
      <div class="search-wrapper">
        <el-button @click="getProjectsChart" icon="el-icon-search" size="mini" >查询</el-button>
      </div>
    </div>
    <common-chart class="common-chart" :chart-type="graphicsType" size="big" :data="chart" v-if="!loading"></common-chart>
  </div>
</template>

<script>
import CommonChart from '@/views/pages/components/CommonChart'
import portal from '@/api/portal'
export default {
  name: 'EnergyChartBuilding',
  data () {
    return {
      loading: true,
      params: {
        page: 1,
        builde_ids: [],
        start_time: null,
        end_time: null
      },
      xqList: [
        {
          text: '中关村',
          value: 1
        },
        {
          text: '良乡',
          value: 2
        }
      ],
      graphicsType: 'line',
      buildingList: [],
      graphicsTypes: [
        {
          text: '柱状图',
          value: 'bar'
        },
        {
          text: '折线图',
          value: 'line'
        }
      ],
      data: [],
      chart: {
        name: '统计分析',
        xAxis: [{ data: [] }],
        series: [],
        legend: []
      }
    }
  },
  components: {
    CommonChart
  },
  created () {
  },
  computed: {
  },
  mounted () {
  },
  methods: {
    changeTimeType () {
      this.params.start_time = null
      this.params.end_time = null
    },
    async getProjectsChart () {
      const params = {
        builde_ids: this.params.builde_id.join(','),
        start_time: this.params.start_time,
        end_time: this.params.end_time
      }
      if (!this.params.is_xq) {
        this.$message.warning('请选择校区')
        return
      }
      if (!this.params.builde_ids) {
        this.$message.warning('请选择楼宇')
        return
      }
      if (!this.params.start_time) {
        this.$message.warning('请选择开始时间')
        return
      }
      if (!this.params.end_time) {
        this.$message.warning('请选择结束时间')
        return
      }
      const res = await portal.getProjectsChart(params)
      if (res.data) {
        this.loading = true
        const records = res.data
        const xAxisData = []
        const addressMap = {}
        records.forEach(e => {
          e.forEach((el, index) => {
            if (!index) {
              xAxisData.push(el.time)
            }
            if (!addressMap[el.builde_name]) {
              addressMap[el.builde_name] = []
            }
            addressMap[el.builde_name].push(el.count)
          })
        })
        const series = []
        const legend = []
        for (const key in addressMap) {
          legend.push(key)
          series.push(
            {
              type: this.graphicsType,
              name: key,
              barWidth: '12%',
              label: {
                show: true,
                position: 'top',
                textStyle: { color: '#fff', fontSize: 14, fontStyle: 'oblique' }
              },
              data: addressMap[key]
            }
          )
        }
        console.log(addressMap)
        this.chart.series = series
        this.chart.xAxis = [
          { data: xAxisData }
        ]
        this.chart.legend = legend
        this.loading = false
      }
    },
    // 根据校区获取楼宇
    async getBuilde () {
      const params = {
        is_xq: this.params.is_xq
      }
      const result = await portal.getBuilde(params)
      if (result.data) {
        result.data.forEach(e => {
          e.value = e.id
          e.label = e.builde_name
          e.check = false
        })
        this.buildingList = result.data
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>
